/************************************************************************
Copyright 2003, 2004, 2005, 2006, 2007, 2008 Grid Technology Research Center,
National Institute of Advanced Industrial Science and Technology.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************/

package jp.aist.gtrc.plus.scheduler.specific.pbs.util;

import java.util.Calendar;
import java.util.Locale;

import jp.aist.gtrc.plus.reserve.ReserveException;
import jp.aist.gtrc.plus.reserve.ReserveId;
import jp.aist.gtrc.plus.reserve.ReserveInfo;
import jp.aist.gtrc.plus.reserve.ReserveManager;
import jp.aist.gtrc.plus.reserve.ReserveRecorder;
import jp.aist.gtrc.plus.scheduler.ScheduleStarter;
import jp.aist.gtrc.plus.scheduler.SchedulerOptions;
import jp.aist.gtrc.plus.scheduler.node.NodeAllocator;
import jp.aist.gtrc.plus.scheduler.node.NodeManager;
import jp.aist.gtrc.plus.scheduler.status.StatusManager;
import jp.aist.gtrc.plus.scheduler.util.TimeUtil;

public class PBSReserveManager extends ReserveManager {

	private final PBSQueueManager qmgr;

	public PBSReserveManager(NodeManager nodeMgr, ScheduleStarter starter,
			ReserveRecorder recorder, NodeAllocator nodeAllocator,
			PBSQueueManager qmgr, SchedulerOptions options,
			StatusManager statusMgr) throws ReserveException {

		super(nodeMgr, starter, recorder, nodeAllocator, options, statusMgr);
		this.qmgr = qmgr;

	}

	private String rsvIdToQueueName(ReserveId rsvId) {

		return rsvId.toString();

	}

	private void createReserveQueue(ReserveId rsvId, Calendar start, Calendar end,
			String owner) throws PBSException {

		String queueName = rsvIdToQueueName(rsvId);
		int d = TimeUtil.calToSec(end) - TimeUtil.calToSec(start);
		String walltime = String.format(Locale.US, "%02d:%02d:%02d",
				d / 3600, (d % 3600) / 60, d % 60);

		try {
			qmgr.connect();
			/*
			 * create queue R1234
			 * set queue R1234 queue_type = Execution
			 * set queue R1234 resources_default.nodes = 1
			 * set queue R1234 resources_default.walltime = 01:00:00
			 * set queue R1234 acl_user_enable = True
			 * set queue R1234 acl_users = userA
			 * set queue R1234 acl_users += userB   [optional]
			 * set queue R1234 enabled = True
			 * set queue R1234 started = True
			 */
			qmgr.createQueue(queueName);
			qmgr.setQueue(queueName, "queue_type", "Execution");
			qmgr.setQueue(queueName, "resources_default", "nodes", "1");
			qmgr.setQueue(queueName, "resources_default", "walltime", walltime);
			qmgr.setQueue(queueName, "acl_user_enable", "True");
			qmgr.setQueue(queueName, "acl_users", owner);
			qmgr.setQueue(queueName, "enabled", "True");
			qmgr.setQueue(queueName, "started", "True");
		} catch (PBSException e) {
			try {
				qmgr.deleteQueue(queueName);
			} catch (PBSException e2) {
				// ignore
			}
			throw e;
		} finally {
			qmgr.disconnect();
		}

	}

	protected void addNewReserveInfo(ReserveInfo rsvInfo) throws ReserveException {

		super.addNewReserveInfo(rsvInfo);

		try {
			createReserveQueue(rsvInfo.getReserveId(), rsvInfo.getStartTime(),
					rsvInfo.getEndTime(), rsvInfo.getOwner());
		} catch (PBSException e) {
			throw new ReserveException(e.getMessage());
		}

	}

	protected void removeReserveInfo(ReserveInfo rsvInfo, boolean forceRemove) throws ReserveException {

		super.removeReserveInfo(rsvInfo, forceRemove);
		deleteReserveQueue(rsvInfo.getReserveId());

	}

	private void deleteReserveQueue(ReserveId rsvId)  {

		try {
			qmgr.connect();
			qmgr.deleteQueue(rsvIdToQueueName(rsvId));
		} catch (PBSException e) {
			// ignore
		} finally {
			qmgr.disconnect();
		}

	}

}
